package com.ado.abyss.core.service.impl;

import com.ado.abyss.core.dal.model.SysRole;
import com.ado.abyss.core.dal.model.SysRoleMenu;
import com.ado.abyss.core.manager.SysRoleManager;
import com.ado.abyss.core.manager.SysRoleMenuManager;
import com.ado.abyss.core.params.page.PageRequest;
import com.ado.abyss.core.service.SysRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author adonais
 * @date 2020/7/10
 * @description SysRoleServiceImpl
 **/
@Service(value = "sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    @Qualifier(value = "sysRoleManager")
    private SysRoleManager sysRoleManager;
    @Autowired
    @Qualifier(value = "sysRoleMenuManager")
    private SysRoleMenuManager sysRoleMenuManager ;

    @Override
    @Transactional
    public boolean saveRoleMenus(List<SysRoleMenu> records) {
        return sysRoleMenuManager.saveRoleMenuBatch(records);
    }

    @Override
    public SysRole selectByPrimaryKey(Long roleId) {
        return sysRoleManager.findById(roleId);
    }

    @Override
    public List<SysRoleMenu> findRoleMenus(Long roleId) {
        return sysRoleMenuManager.findByRoleId(roleId);
    }

    @Override
    public List<SysRoleMenu> findAll() {
        return sysRoleMenuManager.findAll();
    }

    @Override
    public PageInfo<SysRole> findPage(PageRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPageNum() , pageRequest.getPageSize()) ;
        List<SysRole> sysRoles = new ArrayList<SysRole>();
        PageInfo<SysRole> sysRolePageInfo = new PageInfo<SysRole>(sysRoles);
        return sysRolePageInfo;
    }

    @Override
    @Transactional
    public boolean delete(List<SysRole> records) {
        List<Long> ids = new ArrayList<Long>() ;
        for (SysRole sysRole : records) {
            ids.add(sysRole.getId()) ;
        }
        return sysRoleManager.deleteByIds(ids);
    }

    @Override
    @Transactional
    public boolean save(SysRole record) {
        return sysRoleManager.saveRole(record);
    }

    @Override
    public SysRole findById(Long id) {
        return sysRoleManager.findById(id);
    }

    @Override
    public SysRole findByName(String name) {
        return sysRoleManager.findByName(name);
    }
}
